// @Author EthanScriptOn
// @Desc
package custom_exp

import (
	"strings"
)

// MultiError A collection of multiple exceptions
type MultiError struct {
	errors []error
}

// Error Multiple exceptions for the overall formatted output
func (m MultiError) Error() string {
	builder := strings.Builder{}
	for _, err := range m.errors {
		if err != nil {
			builder.WriteString("\r\n")
			builder.WriteString(err.Error())
		}
	}
	return builder.String()
}

// AddError Add an error
func (m *MultiError) AddError(err ...error) {
	for _, errInstance := range err {
		if errInstance == nil {
			continue
		}
		if multiErrorInstance, ok := errInstance.(*MultiError); ok {
			m.errors = append(m.errors, multiErrorInstance.GetErrors()...)
			continue
		}
		m.errors = append(m.errors, errInstance)
	}
}

func GenerateMultiError(err ...error) *MultiError {
	instance := new(MultiError)
	for _, e := range err {
		instance.AddError(e)
	}
	return instance
}

// GetErrors Get all the bugs
func (m *MultiError) GetErrors() []error {
	return m.errors
}
